﻿

using HBase;
using System.Collections.Generic;
namespace hCommand {
    public abstract class AbsDirectionChange :ZtTask {
 
        private int dir;
        private int carrierContr1 = 1;

        public bool canIn() {
            if( carrierContr1 != 1 ) {
                return false;
            }
            return dir == 1;
        }
        public bool canOut() {
            if( carrierContr1 != 1 ) {
                return false;
            }
            return dir == 2;
        }
        public abstract int getDbModel();

        public virtual List<BaseTask> getWorkingCarrierTask() {
            return new List<BaseTask>();
        }

        public virtual bool willInstock() {
            return false;
        }
        public virtual int ReadDir() {
            return dir;
        }

        public virtual bool ignore() {
            return false;
        
        }
        public abstract bool hasTask();
        public abstract void WriteDir(short mode);
        public override bool excute() {
            if (ignore()) {
                return true;
            }
            dir = ReadDir();
            bool b = hasTask();
            if( b ) {
                console(InfoCode.consle_exchange_runed,"层有任务，当前方向为  " + dir);
                //return true;
            }
            int Mode1 = getDbModel();
            List<BaseTask> taskCarriers = getWorkingCarrierTask();
            bool isIn = false;
            bool isOut = false;
            if( willInstock() )//入库口放货需要入库
            {
                isIn = true;
            }
            foreach(BaseTask ct in taskCarriers ) {
                //判断方向
                if (ct.src > 0) {
                    isIn = true;
                } else {
                    isOut = true;
                }
                if( isIn && isOut ) {
                    break;
                }
            }
            isOut = otherNeedOut(isIn ,isOut ,Mode1);
            if( isOut && isIn ) {//口没有被取走 或者 刚刚放上去 即既有入库有用出口 这不变
                return true;
            }

            changeDir(isIn ,isOut ,Mode1);
            return true;
        }
        protected virtual bool otherNeedOut(bool neadIn ,bool neadOut ,int carrierMode1) {
            return neadOut;
        }
        public void changeDir(bool neadIn ,bool neadOut ,int carrierMode1) {
            if( carrierMode1 == 1 ) {
                if( !neadOut ) {
                    changeMode(1);
                }
            } else if( carrierMode1 == 2 ) {
                if( !neadIn ) {
                    changeMode(2);
                }
            } else if( carrierMode1 == 3 ) {
                if( neadIn ) {
                    changeMode(1);
                } else if( neadOut ) {
                    changeMode(2);
                } else {
                    changeMode(1);
                }
            }
        }
        void changeMode(short mode) {
            if( dir == mode ) {
                console(InfoCode.consle_exchange_runed,"层无需切换，当前方向为  " + dir);
                return;
            }
            carrierContr1 = 0;
            WriteDir(mode);
            dir = ReadDir();
            carrierContr1 = 1;
            if( dir == mode ) {
                console(InfoCode.consle_exchange_runed, "切换到！" + mode);
            } else {
                console(InfoCode.consle_exchange_runed, "切换失败！" + dir);
            }
        }
    }
}
